package com.hclc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hclc.ao.AddQuartzAo;
import com.hclc.api.job.IJobAndTriggerService;
import com.hclc.entity.dto.JobAndTriggerDto;
import com.hclc.entity.model.contans.ApiConst;
import com.hclc.entity.model.page.PageUtil;
import com.hclc.entity.model.result.ResultEntity;
import com.hclc.service.QuartzTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @author lixin
 * @version V1.0
 * @ClassName: JobController
 * @Description: TODO(定时任务)
 * @date 2019年9月17日 下午5:56:53
 */

@RestController
@Api(tags = "1.0-quartz", description = "WEB端-定时器接口")
@RequestMapping("/job")
@Validated
@Slf4j
public class JobController {

    @Autowired
    private QuartzTemplateService quartzTemplateService;

    @Autowired
    private IJobAndTriggerService jobAndTriggerService;

    /**
     * 查询所有定时器
     *
     * @param util
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @ApiOperation(value = "查询所有定时器")
    @PostMapping(value = "/getAllQuartz", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<IPage<JobAndTriggerDto>> getQuartz(@ApiParam(value = "分页参数") @RequestBody PageUtil util) {
        Page<JobAndTriggerDto> iPage = new Page(util.getCurrent(), ApiConst.PAGE_SIZE);
        return ResultEntity.ResultSuccess(jobAndTriggerService.getPageJob(iPage));
    }

    /**
     * 添加定时任务
     *
     * @param addQuartzAo
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @ApiOperation(value = "添加定时器")
    @PostMapping(value = "/addQuartz", produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<Boolean> addQuartz(@ApiParam(value = "添加定时参数") @Validated @RequestBody AddQuartzAo addQuartzAo) {
        return ResultEntity.ResultSuccess(quartzTemplateService.addQuartz(addQuartzAo.getType(), addQuartzAo.getCron(), addQuartzAo.getMap()));
    }

    /**
     * 暂停Job
     *
     * @param jobClassName
     * @param jobGroupName
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @ApiOperation(value = "暂停Job")
    @RequestMapping(value = "/pauseQuartz", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<Boolean> pauseQuartz(
            @ApiParam(value = "类名", required = true) @NotBlank(message = "jobClassName不能为空") @RequestParam(value = "jobClassName") String jobClassName,
            @ApiParam(value = "组名", required = true) @NotBlank(message = "jobGroupName不能为空") @RequestParam(value = "jobGroupName") String jobGroupName) {
        return ResultEntity.ResultSuccess(quartzTemplateService.pauseQuartz(jobClassName, jobGroupName));
    }

    /**
     * 恢复Job
     *
     * @param jobClassName
     * @param jobGroupName
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @ApiOperation(value = "恢复Job")
    @RequestMapping(value = "/resumeQuartz", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<Boolean> resumeQuartz(
            @ApiParam(value = "类名", required = true) @NotBlank(message = "jobClassName不能为空") @RequestParam(value = "jobClassName") String jobClassName,
            @ApiParam(value = "组名", required = true) @NotBlank(message = "jobGroupName不能为空") @RequestParam(value = "jobGroupName") String jobGroupName) {
        return ResultEntity.ResultSuccess(quartzTemplateService.resumeQuartz(jobClassName, jobGroupName));
    }

    /**
     * 重新设置Job
     *
     * @param jobClassName
     * @param jobGroupName
     * @param cron
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @ApiOperation(value = "重新设置Job")
    @RequestMapping(value = "/rescheduleQuartz", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<Boolean> rescheduleQuartz(
            @ApiParam(value = "类名", required = true) @NotBlank(message = "jobClassName不能为空") @RequestParam(value = "jobClassName") String jobClassName,
            @ApiParam(value = "组名", required = true) @NotBlank(message = "jobGroupName不能为空") @RequestParam(value = "jobGroupName") String jobGroupName,
            @ApiParam(value = "表达式，如：0/5 * * * * ? (每隔5秒)", required = true) @NotBlank(message = "表达式不能为空") @RequestParam(value = "cron") String cron) {
        return ResultEntity.ResultSuccess(quartzTemplateService.rescheduleQuartz(jobClassName, jobGroupName, cron));
    }

    /**
     * 删除JobIJobAndTriggerService
     *
     * @param jobClassName
     * @param jobGroupName
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @ApiOperation(value = "删除Job")
    @RequestMapping(value = "/delQuartz", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResultEntity<Boolean> delQuartz(
            @ApiParam(value = "类名", required = true) @RequestParam(value = "jobClassName") String jobClassName,
            @ApiParam(value = "组名", required = true) @RequestParam(value = "jobGroupName") String jobGroupName) {
        return ResultEntity.ResultSuccess(quartzTemplateService.delQuartz(jobClassName, jobGroupName));
    }

}
